package ro.msg.quiz.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import ro.msg.quiz.domain.QuestionType;

//AUTHOR: Ervin

//View for adding a new question
@SuppressWarnings("serial")
public class AddNewQuestion extends JFrame {

    private static final int frameHeight = 300;
    private static final int frameWidth = 500;

    private JLabel questionLabel = new JLabel("Question");
    private JTextField questionField = new JTextField();
    private JLabel typeLabel = new JLabel("Type");
    private JComboBox typeComboBox;
    private JLabel answersLabel = new JLabel("Nr. of possible answers", JLabel.CENTER);
    private JTextField answersField = new JTextField(5);
    private JButton cancel = new JButton("Cancel");
    private JButton next = new JButton("Next");

    private JPanel panel = new JPanel();
    
    public AddNewQuestion(String questionText, QuestionType type, int possibleAnsw ) {
	this.setSize(new Dimension(frameWidth, frameHeight));
	this.setTitle("SmartQuest 1.0 - Add new Question");

	// //Box Layout of the window , the components(pan0-pan3) will pe
	// inserted top-down////
	panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

	JPanel panel0 = new JPanel();
	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JPanel panel3 = new JPanel();
	// /////////////////////////////////////////////////////////////////////////////////

	// /////////////////pan 0 containing the questionLabel and textfield
	// below /////////////////
	panel0.setLayout(new GridLayout(0, 1));

	panel0.add(questionLabel);
	questionLabel.setAlignmentY(RIGHT_ALIGNMENT);
	questionField.setSize(new Dimension(3, 1));
	questionField.setHorizontalAlignment(SwingConstants.CENTER);
	questionField.setText(questionText);
	panel0.add(questionField);
	// //////////////////////////////////////////////////////////////////////////

	// ///////pan 1 containing typeLabel and our comboBox where you select
	// your questions type///
	panel1.add(typeLabel);
	String comboBoxFields[] = { "Multiple Choice", "Single Choice", "Text Answer" };
	typeComboBox = new JComboBox(comboBoxFields);
	typeComboBox.selectWithKeyChar(type.toString().charAt(0));
	panel1.add(typeComboBox);
	// ////////////////////////////////////////////////////////////////////////////////

	// /////pan 2 containing answerLabel and the textfield associated to the
	// label/////////////
	panel2.add(answersLabel);
	answersField.setText(Integer.toString(possibleAnsw));
	answersField.setHorizontalAlignment(SwingConstants.CENTER);
	panel2.add(answersField);
	// /////////////////////////////////////////////////////////////////////////////////

	// //////////////////////////////Buttons//////////////////////////////////////////
	panel3.add(cancel);
	panel3.add(next);
	// //////////////////////////////////////////////////////////////////////////////

	// We insert to our main panel called "panel" top down the pan0-pan3
	// with alingment of components
	panel.add(panel0);
	panel.add(panel1);
	panel1.setAlignmentX(Component.RIGHT_ALIGNMENT);
	panel.add(panel2);
	panel2.setAlignmentX(Component.RIGHT_ALIGNMENT);
	panel.add(panel3);
	panel3.setAlignmentX(Component.LEFT_ALIGNMENT);
	// //////////////////////////////////////////////////////////////////////////////////////////

	this.add(panel);
	this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	this.setVisible(true);
    }

    public JButton getCancel() {
	return cancel;
    }

    public JButton getNext() {
	return next;
    }

    public void closeFrame() {
	this.dispose();
    }

    public int getNrOfOptions() {
	return Integer.parseInt(answersField.getText());
    }

    public String getQuestionType() {
	return (String) typeComboBox.getSelectedItem();
    }

    public String getQuestionText() {
	return questionField.getText();
    }
}
